package cn.crce.cloud9.comp;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

/**
 * @author OnCloud9
 * @description
 * @project NRCE-TEST
 * @date 2022年09月13日 下午 09:02
 */
public class Case25 implements ActionListener {
    private JFrame frame;
    private JButton button;
    private JButton saveButton;
    private JTextArea textArea;
    private JFileChooser dia;
    private JPanel buttonPanel;

    public void initGUI() {
        //*********Found**********
        frame = new JFrame("file chooser");

        button = new JButton("open file");
        button.setActionCommand("open");
        //*********Found**********
        button.addActionListener(this);
        saveButton = new JButton("save file");
        saveButton.setActionCommand("save");
        //*********Found**********
        saveButton.addActionListener(this);

        textArea = new JTextArea("", 10, 10);
        buttonPanel = new JPanel();
        dia = new JFileChooser();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        buttonPanel.add(button);
        buttonPanel.add(saveButton);
        frame.getContentPane().add(buttonPanel, BorderLayout.NORTH);
        frame.getContentPane().add(textArea, BorderLayout.CENTER);
        frame.setSize(300, 300);
        //*********Found**********
        frame.setVisible(true);
    }

    //*********Found**********
    public void actionPerformed(ActionEvent event) {
        if (event.getActionCommand().equals("open")) {
            dia.showOpenDialog(frame);
            dia.setVisible(true);
            File file = dia.getSelectedFile();
            String fileName = file.getAbsolutePath();
            textArea.append("path of selected file: " + fileName + "\r\n");
        } else if (event.getActionCommand().equals("save")) {
            dia.showSaveDialog(frame);
            dia.setVisible(true);
            File file = dia.getSelectedFile();
            String fileName = file.getAbsolutePath();
            textArea.append("path of saved file: " + fileName + "\r\n");
        }
    }

    public static void main(String args[]) {
        Case25 example = new Case25();
        example.initGUI();
    }
}
